﻿using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ru.yourpictures.Core.Imaging.Filters
{
    /// <summary>
    /// Contains 16, 24 or 32 bit value
    /// </summary>
    public class Pixel
    {
        private static int BlueMask24 = 255;
        private static int GreenMask24 = 255 * 256;
        private static int RedMask24 = 255 * 256 * 256;

        private int _red = 0;
        private int _green = 0;
        private int _blue = 0;
        private int _alpha = 0;
        private int _value = 0;

        public int Alpha
        {
            get { return _alpha; }
            set { _alpha = value; }
        }

        public int Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public int Blue
        {
            get { return _blue; }
            set { _blue = value; }
        }

        public int Green
        {
            get { return _green; }
            set { _green = value; }
        }

        public int Red
        {
            get { return _red; }
            set { _red = value; }
        }

        public Pixel() { }
        public Pixel(int value, PixelFormat format)
        {
            // For GDI+ value has format BGR
            _value = value;
            if (format == PixelFormat.Format24bppRgb)
            {
                _blue = value & BlueMask24;
                _green = value & GreenMask24;
                _red = value & RedMask24;
            }
            else
            {
                throw new NotImplementedException();
            }
        }
        public Pixel(int red, int green, int blue, int alpha, PixelFormat format)
        {
            _red = red;
            _green = green;
            _blue = blue;

            // For GDI+ construct value as BGR
            if (format == PixelFormat.Format24bppRgb)
            {
                _value = _blue + _green * 256 + red * 256 * 256;
            }
            else
            {
                throw new NotImplementedException();
            }
        }
    }
}
